<!DOCTYPE html>












  


<html class="theme-next muse use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=2"/>
<meta name="theme-color" content="#222">












<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />






















<link href="/lib/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />

<link href="/css/main.css?v=6.4.0" rel="stylesheet" type="text/css" />


  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png?v=6.4.0">


  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png?v=6.4.0">


  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png?v=6.4.0">


  <link rel="mask-icon" href="/images/logo.svg?v=6.4.0" color="#222">









<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/',
    scheme: 'Muse',
    version: '6.4.0',
    sidebar: {"position":"left","display":"post","offset":12,"b2t":false,"scrollpercent":false,"onmobile":false},
    fancybox: false,
    fastclick: false,
    lazyload: false,
    tabs: true,
    motion: {"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"slideDownIn","post_body":"slideDownIn","coll_header":"slideLeftIn","sidebar":"slideUpIn"}},
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>


  




  <meta name="description" content="对zookeeper 的开源框架curator中关于listener的使用不算难，这个地方我整理了2篇比较好的文章，对齐用法作出说明。 参考文章如下所示：  https://blog.csdn.net/wo541075754/article/details/70167841 http://ifeve.com/zookeeper-path-cache/  此文不是原创，是参考后进行了2文的整理和总结">
<meta name="keywords" content="Zookeeper,Listener">
<meta property="og:type" content="article">
<meta property="og:title" content="Zookeeper 的监听">
<meta property="og:url" content="http://yoursite.com/2018/06/01/zookeeper-curator-reprint/index.html">
<meta property="og:site_name" content="ThinkMore">
<meta property="og:description" content="对zookeeper 的开源框架curator中关于listener的使用不算难，这个地方我整理了2篇比较好的文章，对齐用法作出说明。 参考文章如下所示：  https://blog.csdn.net/wo541075754/article/details/70167841 http://ifeve.com/zookeeper-path-cache/  此文不是原创，是参考后进行了2文的整理和总结">
<meta property="og:locale" content="zh-Hans">
<meta property="og:updated_time" content="2018-07-09T02:48:36.944Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="Zookeeper 的监听">
<meta name="twitter:description" content="对zookeeper 的开源框架curator中关于listener的使用不算难，这个地方我整理了2篇比较好的文章，对齐用法作出说明。 参考文章如下所示：  https://blog.csdn.net/wo541075754/article/details/70167841 http://ifeve.com/zookeeper-path-cache/  此文不是原创，是参考后进行了2文的整理和总结">






  <link rel="canonical" href="http://yoursite.com/2018/06/01/zookeeper-curator-reprint/"/>



<script type="text/javascript" id="page.configurations">
  CONFIG.page = {
    sidebar: "",
  };
</script>

  <title>Zookeeper 的监听 | ThinkMore</title>
  






  <script type="text/javascript">
    var _hmt = _hmt || [];
    (function() {
      var hm = document.createElement("script");
      hm.src = "https://hm.baidu.com/hm.js?13172698bcdb7ab00b7d3ec112aae9c1";
      var s = document.getElementsByTagName("script")[0];
      s.parentNode.insertBefore(hm, s);
    })();
  </script>




  <noscript>
  <style type="text/css">
    .use-motion .motion-element,
    .use-motion .brand,
    .use-motion .menu-item,
    .sidebar-inner,
    .use-motion .post-block,
    .use-motion .pagination,
    .use-motion .comments,
    .use-motion .post-header,
    .use-motion .post-body,
    .use-motion .collection-title { opacity: initial; }

    .use-motion .logo,
    .use-motion .site-title,
    .use-motion .site-subtitle {
      opacity: initial;
      top: initial;
    }

    .use-motion {
      .logo-line-before i { left: initial; }
      .logo-line-after i { right: initial; }
    }
  </style>
</noscript>

</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  
  
    
  

  <div class="container sidebar-position-left page-post-detail">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/" class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">ThinkMore</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
    
      
        <p class="site-subtitle">驽马十驾,功在不舍！</p>
      
    
  </div>

  <div class="site-nav-toggle">
    <button aria-label="Toggle navigation bar">
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>



<nav class="site-nav">
  
    <ul id="menu" class="menu">
      
        
        
        
          
          <li class="menu-item menu-item-home">
    <a href="/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-home"></i> <br />Home</a>
  </li>
        
        
        
          
          <li class="menu-item menu-item-archives">
    <a href="/archives/" rel="section">
      <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />Archives</a>
  </li>

      
      
    </ul>
  

  
    

  

  
</nav>



  



</div>
    </header>

    


    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  

  <article class="post post-type-normal" itemscope itemtype="http://schema.org/Article">
  
  
  
  <div class="post-block">
    <link itemprop="mainEntityOfPage" href="http://yoursite.com/2018/06/01/zookeeper-curator-reprint/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="文敦复">
      <meta itemprop="description" content="思考的懒惰是真的懒惰！">
      <meta itemprop="image" content="/images/avatar.gif">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="ThinkMore">
    </span>

    
      <header class="post-header">

        
        
          <h1 class="post-title" itemprop="name headline">Zookeeper 的监听
              
            
          </h1>
        

        <div class="post-meta">
          <span class="post-time">

            
            
            

            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">Posted on</span>
              

              
                
              

              <time title="Created: 2018-06-01 20:39:30" itemprop="dateCreated datePublished" datetime="2018-06-01T20:39:30+08:00">2018-06-01</time>
            

            
              

              
                
                <span class="post-meta-divider">|</span>
                

                <span class="post-meta-item-icon">
                  <i class="fa fa-calendar-check-o"></i>
                </span>
                
                  <span class="post-meta-item-text">Edited on</span>
                
                <time title="Modified: 2018-07-09 10:48:36" itemprop="dateModified" datetime="2018-07-09T10:48:36+08:00">2018-07-09</time>
              
            
          </span>

          
            <span class="post-category" >
            
              <span class="post-meta-divider">|</span>
            
              <span class="post-meta-item-icon">
                <i class="fa fa-folder-o"></i>
              </span>
              
                <span class="post-meta-item-text">In</span>
              
              
                <span itemprop="about" itemscope itemtype="http://schema.org/Thing"><a href="/categories/Zookeeper/" itemprop="url" rel="index"><span itemprop="name">Zookeeper</span></a></span>

                
                
              
            </span>
          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    
    
    
    <div class="post-body" itemprop="articleBody">

      
      

      
        <p>对<code>zookeeper</code> 的开源框架<code>curator</code>中关于<code>listener</code>的使用不算难，这个地方我整理了2篇比较好的文章，对齐用法作出说明。</p>
<p>参考文章如下所示：</p>
<ul>
<li><a href="https://blog.csdn.net/wo541075754/article/details/70167841" target="_blank" rel="noopener">https://blog.csdn.net/wo541075754/article/details/70167841</a></li>
<li><a href="http://ifeve.com/zookeeper-path-cache/" target="_blank" rel="noopener">http://ifeve.com/zookeeper-path-cache/</a></li>
</ul>
<p>此文不是原创，是参考后进行了2文的整理和总结，如果文章有说明的不清晰的地方，请参考原文出处。</p>
<blockquote>
<p>三更灯火五更鸡，正是男儿读书时。 </p>
</blockquote>
<a id="more"></a>
<h1 id="引入-Maven"><a href="#引入-Maven" class="headerlink" title="引入 Maven"></a>引入 Maven</h1><figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.zookeeper<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>zookeeper<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>3.4.6<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line">    <span class="comment">&lt;!--此处需要排除 log4j2 的引导，因为当前项目已使用logback--&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">exclusions</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;<span class="name">exclusion</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>slf4j-log4j12<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">            <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.slf4j<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">        <span class="tag">&lt;/<span class="name">exclusion</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;/<span class="name">exclusions</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-framework<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-client<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br><span class="line"><span class="comment">&lt;!--对监听的操作封装到了此库中--&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-recipes<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<h1 id="常见错误"><a href="#常见错误" class="headerlink" title="常见错误"></a>常见错误</h1><p>在进正文前，写将我在写这个博客过程中发现的一些旁枝末节的问题告诉大家：</p>
<ol>
<li><code>zookeeper</code>在创建节点之前，请一定要判定其节点是否存放，否则可能会报错：<code>org.apache.zookeeper.KeeperException$NodeExistsException:</code>,核心错误是：<code>NodeExistsException</code>。</li>
<li><code>zookeeper</code>中如果使用<code>usingWatcher</code>的时候，请注意你监听的节点必须是已经存在的，否则会抛出错误<code>org.apache.zookeeper.KeeperException$NodeExistsException:</code>，核心错误是<code>NodeExists</code></li>
<li>临时节点下不能再挂节点，只有持久化节点下才能挂节点（临时和非临时都可）</li>
</ol>
<h1 id="监听一-usingWatcher"><a href="#监听一-usingWatcher" class="headerlink" title="监听一:usingWatcher"></a>监听一:usingWatcher</h1><p>直接先上代码：</p>
<ol>
<li>设置监听</li>
</ol>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">byte</span>[] content = client.getData().usingWatcher(<span class="keyword">new</span> Watcher() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent watchedEvent)</span> </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"监听器watchedEvent："</span> + watchedEvent);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;).forPath(path);</span><br></pre></td></tr></table></figure>
<p>通过源码查看可以发现 <code>WatchedEvent</code>中有如下事件：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">enum</span> EventType &#123;</span><br><span class="line">          None (-<span class="number">1</span>),</span><br><span class="line">          NodeCreated (<span class="number">1</span>), <span class="comment">//节点创建</span></span><br><span class="line">          NodeDeleted (<span class="number">2</span>), <span class="comment">//节点删除</span></span><br><span class="line">          NodeDataChanged (<span class="number">3</span>), <span class="comment">//节点数据改变</span></span><br><span class="line">          NodeChildrenChanged (<span class="number">4</span>); <span class="comment">//子节点改变</span></span><br><span class="line">			<span class="comment">//省略部分代码...</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>由此可见其支持的事件还是很多的，假如我使用如下代码来改变节点数据：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//设置获取修改节点</span></span><br><span class="line">client.setData().forPath(path, <span class="string">"Hicode"</span>.getBytes());</span><br><span class="line">ThreadUtil.sleep(<span class="number">2</span>, TimeUnit.SECONDS);</span><br><span class="line"></span><br><span class="line">client.setData().forPath(path, <span class="string">"Club"</span>.getBytes());</span><br><span class="line">ThreadUtil.sleep(<span class="number">100</span>, TimeUnit.SECONDS);</span><br></pre></td></tr></table></figure>
<p>你可以发现，监听只会触发一次，只会输出一次。</p>
<p>这个也确实是该用法的一个问题，每个事件监听只会触发一次，如果需要反复触发，需要在每次进行操作前再添加一个监听，代码如下所示，通过这个案例你会发现使用 <code>usingWatcher</code>会比较繁琐：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br></pre></td><td class="code"><pre><span class="line">     <span class="comment">// 第一次注册监听</span></span><br><span class="line">     client.getData().usingWatcher(<span class="keyword">new</span> Watcher() &#123;</span><br><span class="line">          <span class="meta">@Override</span></span><br><span class="line">          <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent watchedEvent)</span> </span>&#123;</span><br><span class="line">              System.out.println(<span class="string">"监听器watchedEvent："</span> + watchedEvent);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;).forPath(path);</span><br><span class="line"></span><br><span class="line">      <span class="comment">//设置获取修改节点</span></span><br><span class="line">      System.out.println(<span class="string">"第一次改变"</span>);</span><br><span class="line">      client.setData().forPath(path, <span class="string">"Hicode"</span>.getBytes());</span><br><span class="line"></span><br><span class="line"><span class="comment">// 第二次注册监听</span></span><br><span class="line">      client.getData().usingWatcher(<span class="keyword">new</span> Watcher() &#123;</span><br><span class="line">          <span class="meta">@Override</span></span><br><span class="line">          <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">process</span><span class="params">(WatchedEvent watchedEvent)</span> </span>&#123;</span><br><span class="line">              System.out.println(<span class="string">"监听器watchedEvent："</span> + watchedEvent);</span><br><span class="line">          &#125;</span><br><span class="line">      &#125;).forPath(path);</span><br><span class="line">      ThreadUtil.sleep(<span class="number">2</span>, TimeUnit.SECONDS);</span><br><span class="line"></span><br><span class="line">      System.out.println(<span class="string">"第二次改变"</span>);</span><br><span class="line">      client.setData().forPath(path, <span class="string">"Club"</span>.getBytes());</span><br><span class="line">      ThreadUtil.sleep(<span class="number">12</span>, TimeUnit.SECONDS);</span><br></pre></td></tr></table></figure>
<p>总结下：</p>
<ol>
<li><code>usingWatcher</code>只会触发一次，每一次被触发后，需要再次注册监听才行。</li>
</ol>
<h1 id="监听二-CuratorListener"><a href="#监听二-CuratorListener" class="headerlink" title="监听二:CuratorListener"></a>监听二:CuratorListener</h1><p>还是先上代码，如下代码表示注册监听</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">CuratorListener listener = <span class="keyword">new</span> CuratorListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">eventReceived</span><span class="params">(CuratorFramework client, CuratorEvent event)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"监听事件触发，event事件为："</span> + event.getType());</span><br><span class="line">            &#125;</span><br><span class="line">client.getCuratorListenable().addListener(listener);</span><br></pre></td></tr></table></figure>
<p>假设我们有如下代码来改变状态：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//创建临时子节点</span></span><br><span class="line">client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).inBackground().forPath(path + <span class="string">"/hi"</span>);</span><br><span class="line"><span class="comment">// 异步获取节点数据</span></span><br><span class="line">client.getData().inBackground().forPath(path);</span><br><span class="line"><span class="comment">// 变更节点内容inBackground 。</span></span><br><span class="line">client.setData().forPath(path, <span class="string">"124443"</span>.getBytes());</span><br><span class="line"><span class="comment">//创建临时子节点</span></span><br><span class="line">client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path + <span class="string">"/hi"</span>);</span><br><span class="line">client.delete().deletingChildrenIfNeeded().forPath(path);</span><br></pre></td></tr></table></figure>
<p>仔细观察结果可以发现：</p>
<figure class="highlight verilog"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">监听事件触发，<span class="keyword">event</span>事件为：WATCHED</span><br><span class="line">监听事件触发，<span class="keyword">event</span>事件为：GET_DATA</span><br><span class="line">监听事件触发，<span class="keyword">event</span>事件为：CREATE</span><br></pre></td></tr></table></figure>
<p>通过输出，我们可以发现，监听一共触发了3次。</p>
<ol>
<li>第一次是默认肯定会触发的，因为他的<code>type</code>为<code>watched</code>表示被监听。</li>
<li>第二次是<code>GET_DATA</code>获取数据，结合代码可以看出上<code>client.getData()</code>触发的。</li>
<li>第三次是<code>CREATE</code>创建事件，通过比对代代码可以看出是因为<code>create</code>触发的。</li>
</ol>
<p>你会不会有疑问，为什么<code>setData()</code>和<code>delete()</code>没有触发，是因为不支持嘛？</p>
<p>如果你仔细你可以发现，触发了事件监听的都是因为一句关键代码<code>inBackground</code>。你可以试试在没有触发的代码中加入这个试试。</p>
<p>那么他支持哪些事件了，通过查看源码可以分析出:</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">package</span> org.apache.curator.framework.api;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="keyword">enum</span> CuratorEventType &#123;</span><br><span class="line">    CREATE,</span><br><span class="line">    DELETE,</span><br><span class="line">    EXISTS,</span><br><span class="line">    GET_DATA,</span><br><span class="line">    SET_DATA,</span><br><span class="line">    CHILDREN,</span><br><span class="line">    SYNC,</span><br><span class="line">    GET_ACL,</span><br><span class="line">    SET_ACL,</span><br><span class="line">    TRANSACTION,</span><br><span class="line">    GET_CONFIG,</span><br><span class="line">    RECONFIG,</span><br><span class="line">    WATCHED,</span><br><span class="line">    REMOVE_WATCHES,</span><br><span class="line">    CLOSING;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>几乎所有的事件都是支持的，前提是通过 <code>inbackground</code>在后台运行。</p>
<h1 id="监听三：Cache-监听"><a href="#监听三：Cache-监听" class="headerlink" title="监听三：Cache 监听"></a>监听三：Cache 监听</h1><p>在使用此方式的时候，是需要引入这个 jar 包的。</p>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">dependency</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">groupId</span>&gt;</span>org.apache.curator<span class="tag">&lt;/<span class="name">groupId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">artifactId</span>&gt;</span>curator-recipes<span class="tag">&lt;/<span class="name">artifactId</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">version</span>&gt;</span>4.0.0<span class="tag">&lt;/<span class="name">version</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">dependency</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>他是通过缓存的节点数据的变更来完成监听的，在我的理解中是通过缓存数据的变更来触发事件的，并不是直接接听服务节点。</p>
<h2 id="NodeCache"><a href="#NodeCache" class="headerlink" title="NodeCache"></a>NodeCache</h2><p>该缓存监听的是节点自身的变化，比如说创建、变更和删除。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">    <span class="meta">@Test</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">nodeCacheTest</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        String path = <span class="string">"/hicode/club"</span>;</span><br><span class="line">        CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(<span class="keyword">new</span> RetryNTimes(<span class="number">5</span>, <span class="number">2000</span>)) .connectString(CONNECT_STR).build();</span><br><span class="line">        client.start();</span><br><span class="line">        <span class="comment">//通过 client 创建 nodeCache，请注意一定要 start</span></span><br><span class="line">        NodeCache nodeCache = <span class="keyword">new</span> NodeCache(client, path);</span><br><span class="line">        nodeCache.start();</span><br><span class="line">        nodeCache.getListenable().addListener(<span class="keyword">new</span> NodeCacheListener() &#123;</span><br><span class="line">            <span class="meta">@Override</span></span><br><span class="line">            <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">nodeChanged</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">                System.out.println(<span class="string">"nodeChanged..."</span>);</span><br><span class="line">            &#125;</span><br><span class="line">        &#125;);</span><br><span class="line">client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path+<span class="string">"/"</span>+<span class="string">"test"</span>);</span><br><span class="line">        client.setData().forPath(path+<span class="string">"/test"</span>,<span class="string">"cc"</span>.getBytes());</span><br><span class="line">        client.setData().forPath(path+<span class="string">"/test"</span>,<span class="string">"hh"</span>.getBytes());</span><br><span class="line">        client.delete().forPath(path+<span class="string">"/test"</span>);</span><br><span class="line">        ThreadUtil.sleep(<span class="number">5</span>,TimeUnit.MINUTES);</span><br><span class="line">        nodeCache.close();</span><br><span class="line">        client.close();</span><br><span class="line">    &#125;</span><br></pre></td></tr></table></figure>
<p>上述代码如下代码是构建缓存并创建监听：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment">//通过 client 创建 nodeCache，请注意一定要 start</span></span><br><span class="line">NodeCache nodeCache = <span class="keyword">new</span> NodeCache(client, path);</span><br><span class="line">nodeCache.start();</span><br><span class="line">nodeCache.getListenable().addListener(<span class="keyword">new</span> NodeCacheListener() &#123;</span><br><span class="line">    <span class="meta">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">nodeChanged</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        System.out.println(<span class="string">"nodeChanged..."</span>);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>
<p>最终输出的只有一句：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">nodeChanged...</span><br></pre></td></tr></table></figure>
<p>刚好和我前文提到的：<code>NodeCache</code>只会监听节点自身的变更。</p>
<h2 id="PathCache"><a href="#PathCache" class="headerlink" title="PathCache"></a>PathCache</h2><p>该缓存变相监听的是节点的路径的变更，比如说因为子节点创建导致路径变长，子节点删除导致路径变短，对节点自身的值改变不做监听。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">pathCacheTest</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">    String path = <span class="string">"/hicode/club/childPath"</span>;</span><br><span class="line">    CuratorFramework client = CuratorFrameworkFactory.builder()</span><br><span class="line">        .retryPolicy(<span class="keyword">new</span> RetryNTimes(<span class="number">5</span>, <span class="number">2000</span>)) .connectString(CONNECT_STR).build();</span><br><span class="line">    client.start();</span><br><span class="line">    PathChildrenCache pathCache = <span class="keyword">new</span> PathChildrenCache(client, path, <span class="keyword">false</span>);</span><br><span class="line">    pathCache.start();</span><br><span class="line">    <span class="comment">//默认会有事件：PathChildrenCacheEvent&#123;type=CONNECTION_RECONNECTED, data=null&#125;</span></span><br><span class="line">    pathCache.getListenable().addListener(<span class="keyword">new</span> PathChildrenCacheListener() &#123;</span><br><span class="line">        <span class="meta">@Override</span></span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">childEvent</span><span class="params">(CuratorFramework client, PathChildrenCacheEvent event)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">            System.out.println(<span class="string">"事件类型是："</span> + event);</span><br><span class="line">        &#125;</span><br><span class="line">    &#125;);</span><br><span class="line">    <span class="comment">//创建节点，不触发</span></span><br><span class="line">    client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT)</span><br><span class="line">        .forPath(path);</span><br><span class="line">    <span class="comment">//对节点赋值，不触发</span></span><br><span class="line">    client.setData().forPath(path, <span class="string">"childPath"</span>.getBytes());</span><br><span class="line">    String tempPath = path + <span class="string">"/temp"</span>;</span><br><span class="line">    <span class="comment">//创建节点触发</span></span><br><span class="line">    client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL)</span><br><span class="line">        .forPath(tempPath)；</span><br><span class="line">    client.setData().forPath(tempPath, <span class="string">"666"</span>.getBytes());</span><br><span class="line">    ThreadUtil.sleep(<span class="number">5</span>, TimeUnit.MINUTES);</span><br><span class="line">    pathCache.close();</span><br><span class="line">    client.close();</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>
<p>输出结果为：</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">事件类型是：PathChildrenCacheEvent&#123;type=CONNECTION_RECONNECTED, data=null&#125;</span><br><span class="line">事件类型是：PathChildrenCacheEvent&#123;type=CHILD_ADDED, data=ChildData&#123;path='/hicode/club/childPath/temp', stat=468,469,1527855307449,1527855307451,1,0,0,72057686640754862,3,0,468</span><br><span class="line">, data=null&#125;&#125;</span><br></pre></td></tr></table></figure>
<p>第一个事件：<code>CONNECTION_RECONNECTED</code>是属于必定触发的。</p>
<p>第二个事件：<code>CHILD_ADDED</code>是增加子节点的时候触发的。</p>
<p>其他的比如在设置节点数据等的时候，事件未触发，符合我们了解到的资料。</p>
<blockquote>
<p>注意代码中最开始监听的是：  String path = “/hicode/club/childPath”; 那么后续的事件触发都是该节点下的。</p>
</blockquote>
<h2 id="TreeCache"><a href="#TreeCache" class="headerlink" title="TreeCache"></a>TreeCache</h2><p><code>TreeCache</code> 属于上述2者的结合体，既监听了节点自身的变化，又监听了节点路径的变化。</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">@Test</span></span><br><span class="line"> <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">treeCacheTest</span><span class="params">()</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">     String path = <span class="string">"/hicode/club/tree"</span>;</span><br><span class="line">     CuratorFramework client = CuratorFrameworkFactory.builder().retryPolicy(<span class="keyword">new</span> RetryNTimes(<span class="number">5</span>, <span class="number">2000</span>))</span><br><span class="line">             .connectString(CONNECT_STR).build();</span><br><span class="line">     client.start();</span><br><span class="line">     TreeCache treeCache = <span class="keyword">new</span> TreeCache(client, path);</span><br><span class="line">     treeCache.start();</span><br><span class="line">     <span class="comment">//默认会有事件：PathChildrenCacheEvent&#123;type=CONNECTION_RECONNECTED, data=null&#125;</span></span><br><span class="line">     treeCache.getListenable().addListener(<span class="keyword">new</span> TreeCacheListener() &#123;</span><br><span class="line">         <span class="meta">@Override</span></span><br><span class="line">         <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">childEvent</span><span class="params">(CuratorFramework client, TreeCacheEvent event)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">             System.out.println(<span class="string">"事件类型是："</span> + event);</span><br><span class="line">         &#125;</span><br><span class="line">     &#125;);</span><br><span class="line">     client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(path + <span class="string">"/t1"</span>, <span class="string">"xxx"</span>.getBytes());</span><br><span class="line">     <span class="comment">//client.setData().forPath(path, "childPath".getBytes());</span></span><br><span class="line">     ThreadUtil.sleep(<span class="number">1</span>, TimeUnit.SECONDS);</span><br><span class="line">     System.out.println(<span class="string">"=============="</span>);</span><br><span class="line">     String tempPath = path + <span class="string">"/t2"</span>;</span><br><span class="line">     client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath(tempPath, <span class="string">"xxx"</span>.getBytes());</span><br><span class="line">     client.setData().forPath(tempPath, <span class="string">"666"</span>.getBytes());</span><br><span class="line">     client.delete().forPath(tempPath);</span><br><span class="line">     ThreadUtil.sleep(<span class="number">5</span>, TimeUnit.MINUTES);</span><br><span class="line">     treeCache.close();</span><br><span class="line">     client.close();</span><br><span class="line"> &#125;</span><br></pre></td></tr></table></figure>
<p>结果为:</p>
<figure class="highlight shell"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><span class="line">事件类型是：TreeCacheEvent&#123;type=NODE_ADDED, data=ChildData&#123;path='/hicode/club/tree', stat=566,566,1527856851406,1527856851406,0,11,0,0,0,1,589</span><br><span class="line">, data=[]&#125;&#125;</span><br><span class="line">事件类型是：TreeCacheEvent&#123;type=NODE_ADDED, data=ChildData&#123;path='/hicode/club/tree/t1', stat=589,589,1527857005459,1527857005459,0,0,0,72057686640754891,3,0,589</span><br><span class="line">, data=[120, 120, 120]&#125;&#125;</span><br><span class="line">事件类型是：TreeCacheEvent&#123;type=INITIALIZED, data=null&#125;</span><br><span class="line">==============</span><br><span class="line">事件类型是：TreeCacheEvent&#123;type=NODE_ADDED, data=ChildData&#123;path='/hicode/club/tree/t2', stat=590,590,1527857006474,1527857006474,0,0,0,72057686640754891,3,0,590</span><br><span class="line">, data=[120, 120, 120]&#125;&#125;</span><br><span class="line">事件类型是：TreeCacheEvent&#123;type=NODE_UPDATED, data=ChildData&#123;path='/hicode/club/tree/t2', stat=590,591,1527857006474,1527857006483,1,0,0,72057686640754891,3,0,590</span><br><span class="line">, data=[54, 54, 54]&#125;&#125;</span><br><span class="line">事件类型是：TreeCacheEvent&#123;type=NODE_REMOVED, data=ChildData&#123;path='/hicode/club/tree/t2', stat=590,591,1527857006474,1527857006483,1,0,0,72057686640754891,3,0,590</span><br><span class="line">, data=[54, 54, 54]&#125;&#125;</span><br></pre></td></tr></table></figure>
<p><code>TreeCache</code>可以理解为<code>NodeCache</code>+<code>PathCache</code>的集合。</p>
<h1 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h1><p>如上就是 <code>Zookeeper</code>常用的用于节点监听的方式，over。</p>
<blockquote>
<p>您有任何建议和意见，请Email联系: <a href="mailto:hicode_club@163.com" target="_blank" rel="noopener">hicode_club@163.com</a></p>
<p>转载请保留出处 <a href="http://www.hicode.club" target="_blank" rel="noopener">HiCode 俱乐部</a></p>
</blockquote>

      
    </div>

    

    
    
    

    

    

    

    <footer class="post-footer">
      

      
      
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/2018/05/27/classloader/" rel="next" title="Java类加载器--ClassLoader">
                <i class="fa fa-chevron-left"></i> Java类加载器--ClassLoader
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/2018/06/03/centos-start-sh/" rel="prev" title="CentOS 开机启动指定 jar 包">
                CentOS 开机启动指定 jar 包 <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </div>
  
  
  
  </article>


  </div>


          </div>
          

  



        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap">
            Table of Contents
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview-wrap">
            Overview
          </li>
        </ul>
      

      <section class="site-overview-wrap sidebar-panel">
        <div class="site-overview">
          <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
            
              <p class="site-author-name" itemprop="name">文敦复</p>
              <p class="site-description motion-element" itemprop="description">思考的懒惰是真的懒惰！</p>
          </div>

          
            <nav class="site-state motion-element">
              
                <div class="site-state-item site-state-posts">
                
                  <a href="/archives/">
                
                    <span class="site-state-item-count">39</span>
                    <span class="site-state-item-name">posts</span>
                  </a>
                </div>
              

              
                
                
                <div class="site-state-item site-state-categories">
                  
                    
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                      
                    
                    <span class="site-state-item-count">37</span>
                    <span class="site-state-item-name">categories</span>
                  
                </div>
              

              
            </nav>
          

          

          

          
          

          
          

          
            
          
          

        </div>
      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#引入-Maven"><span class="nav-number">1.</span> <span class="nav-text">引入 Maven</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#常见错误"><span class="nav-number">2.</span> <span class="nav-text">常见错误</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#监听一-usingWatcher"><span class="nav-number">3.</span> <span class="nav-text">监听一:usingWatcher</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#监听二-CuratorListener"><span class="nav-number">4.</span> <span class="nav-text">监听二:CuratorListener</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#监听三：Cache-监听"><span class="nav-number">5.</span> <span class="nav-text">监听三：Cache 监听</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#NodeCache"><span class="nav-number">5.1.</span> <span class="nav-text">NodeCache</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#PathCache"><span class="nav-number">5.2.</span> <span class="nav-text">PathCache</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#TreeCache"><span class="nav-number">5.3.</span> <span class="nav-text">TreeCache</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#总结"><span class="nav-number">6.</span> <span class="nav-text">总结</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright">&copy; <span itemprop="copyrightYear">2018</span>
  <span class="with-love" id="animate">
    <i class="fa fa-user"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">文敦复</span>

  

  
</div>




  <div class="powered-by">Powered by <a class="theme-link" target="_blank" href="https://hexo.io">Hexo</a> v3.7.1</div>



  <span class="post-meta-divider">|</span>



  <div class="theme-info">Theme – <a class="theme-link" target="_blank" href="https://theme-next.org">NexT.Muse</a> v6.4.0</div>




        








        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

    
	
    

    
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>


























  
  
    <script type="text/javascript" src="/lib/jquery/index.js?v=2.1.3"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.min.js?v=1.2.1"></script>
  

  
  
    <script type="text/javascript" src="/lib/velocity/velocity.ui.min.js?v=1.2.1"></script>
  


  


  <script type="text/javascript" src="/js/src/utils.js?v=6.4.0"></script>

  <script type="text/javascript" src="/js/src/motion.js?v=6.4.0"></script>



  
  

  
  <script type="text/javascript" src="/js/src/scrollspy.js?v=6.4.0"></script>
<script type="text/javascript" src="/js/src/post-details.js?v=6.4.0"></script>



  


  <script type="text/javascript" src="/js/src/bootstrap.js?v=6.4.0"></script>



  



  










  





  

  

  

  

  
  

  

  

  

  

  

</body>
</html>
